package com.wandian.texas.command.texas;

import com.wandian.texas.BaseCommandObject;
import com.wandian.texas.CommandObject;
import com.wandian.texas.GameData;
import com.wandian.texas.RoomMgr;
import com.wandian.texas.UserMgr;
import com.wandian.command.Cmd;
import com.wandian.command.UserCommand;
import com.wandian.texas.protocol.Protocol;
import com.wandian.xml.RoomConfigItem;

@Cmd(code = Protocol.RoomListCmd, desc = "")
public class RoomListCmd extends UserCommand {

	@Override
	public void execute(BaseCommandObject cmdObject){
		super.execute(cmdObject);
		byte type = cmdObject.readByte();// cmdObject.dataPacket.byteBuffer.get();
		//type=0,获取全部房间信息
		switch (type) {
		case 0:
			sendAllRoom(cmdObject);
			break;

		default:
			break;
		}
	}

	/**
	 * 将房间列表发送过去
	 * 
	 * @param cmdObject
	 */
	private void sendAllRoom(BaseCommandObject cmdObject) {
		int roomCount = RoomMgr.RoomSize();
		cmdObject.writeBegin();
		cmdObject.writeInt(roomCount);//房间总数
		cmdObject.writeByte((byte)GameData.getFloat("RoomCountPerFloor"));//1为开启房间命名
		cmdObject.writeInt(GameData.getInt("RoomCountPerFloor"));//每层的房间数
		cmdObject.writeInt(RoomMgr.bettime);//下注的时间
		cmdObject.writeInt(GameData.ROOMSEAT);//seat
		cmdObject.writeFloat(GameData.ROOMMINBLIND);//ROOMMINBLIND
		cmdObject.writeFloat(GameData.ROOMMAXBLIND);//ROOMMAXBLIND
		cmdObject.writeFloat(GameData.ROOMMINTAKE);//ROOMMINTAKE
		cmdObject.writeFloat(GameData.ROOMMAXTAKE);//ROOMMAXTAKE
		int roomLength = cmdObject.position();
		cmdObject.writeInt(0);//房间总数
		RoomConfigItem room;
		int notEmptyCount = 0;
		for (int i = 1; i <= roomCount; i++) {
			room = RoomMgr.getRoom(i);
			if (room.roomUserSize() == 0) {
				continue;
			}
			cmdObject.writeInt(room.id);
			cmdObject.writeString(room.getName());
			cmdObject.writeFloat(room.minBlind);
			cmdObject.writeFloat(room.maxBlind);
			cmdObject.writeFloat(room.minTake);
			cmdObject.writeFloat(room.maxTake);
			cmdObject.writeInt(room.seat);
			cmdObject.writeInt(room.roomSeatSize());
			//			cmdObject.writeInt(room.maxplayer);
//			cmdObject.writeInt(room.vip);
//			cmdObject.writeInt(room.isLock ? 1 : 0);
//			cmdObject.writeString(room.pwd);
			notEmptyCount++;
		}
		int endPositon = cmdObject.position();
		cmdObject.position(roomLength);
		cmdObject.writeInt(notEmptyCount);
		cmdObject.position(endPositon);
		cmdObject.writeEnd();
		UserMgr.I().SendData(cmdObject);
	}
}
